//
//  HomeMge.m
//  BGM
//
//  Created by liuqin on 2018/11/26.
//  Copyright © 2018年 com.alirtc.demo. All rights reserved.
//

#import "HomeMge.h"
#import "Common.h"
#import "CommonBtn.h"
#import "SVProgressHUD.h"
@interface HomeMge()

@property (nonatomic,strong)UILabel *nameLabel;
@property (nonatomic,strong)CommonBtn *addressBtn;
@property (nonatomic,strong)CommonBtn *timeBtn;

@property (nonatomic,strong)UIButton *iconBtn;
@property (nonatomic,strong)UILabel *titleLabel;

@property (nonatomic,strong)UILabel *contentLabel;
@property (nonatomic,strong)UILabel *reletionShipLabel;


@property (nonatomic,strong)CommonBtn *commentBtn;
@property (nonatomic,strong)CommonBtn *forwardBtn;

@property (nonatomic,strong)UIButton *leftBtn;

@property (nonatomic,strong)UILabel *red1;  //帮转红包
@property (nonatomic,strong)UILabel *red2;  //有偿

@property (nonatomic,strong)NSDictionary *messgeDic;

@end

@implementation HomeMge


-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self != nil) {
        
        
        UIImageView *bgImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        [bgImage setImage:[UIImage imageNamed:@"HomeCard"]];
        [self addSubview:bgImage];
        float h = (bgImage.frame.size.height-40)/6;
        

        
        self.nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 5, frame.size.width, h/2)];
        self.nameLabel.font = [UIFont systemFontOfSize:17];
        self.nameLabel.textAlignment = NSTextAlignmentCenter;
        self.nameLabel.textColor = bgmmainColor;
        self.nameLabel.text = @"张培林";
        [self addSubview:self.nameLabel];
        
        float x = (frame.size.width-120)/2;
        self.addressBtn = [[CommonBtn alloc]initWithFrame:CGRectMake(x, h/2, 60, h/2)];
        [self.addressBtn setImage:[UIImage imageNamed:@"adress"] forState:UIControlStateNormal];
        [self.addressBtn setTitle:@"杭州市" forState:UIControlStateNormal];
        [self addSubview:self.addressBtn];

        self.timeBtn = [[CommonBtn alloc]initWithFrame:CGRectMake(x+60, h/2, 60, h/2)];
        [self.timeBtn setImage:[UIImage imageNamed:@"time"] forState:UIControlStateNormal];
        [self.timeBtn setTitle:@"三天前" forState:UIControlStateNormal];
        [self addSubview:self.timeBtn];
        UILabel *line1 = [[UILabel alloc]initWithFrame:CGRectMake(5, h, frame.size.width-10, 0.3)];
        line1.backgroundColor = [UIColor grayColor];
        line1.alpha = 0.3;
        [self addSubview:line1];
        
        self.iconBtn = [[UIButton alloc]initWithFrame:CGRectMake(15, line1.frame.origin.y, h, h)];
        [self addSubview:self.iconBtn];
        
        self.titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(h+15, line1.frame.origin.y, frame.size.width-(h+15)-15, h)];
        self.titleLabel.text = @"寻找杭州上城区铺面一面，求帮忙";
        self.titleLabel.font = [UIFont boldSystemFontOfSize:18];
        self.titleLabel.textColor = bgmmainColor;
        [self addSubview:self.titleLabel];
        
        UILabel *line2 = [[UILabel alloc]initWithFrame:CGRectMake(5, h*2, frame.size.width-10, 0.3)];
        line2.backgroundColor = [UIColor grayColor];
        line2.alpha = 0.3;
        [self addSubview:line2];
        
        UIButton *messageBtn = [[UIButton alloc]initWithFrame:CGRectMake(15, line2.frame.origin.y+20, h, h)];
        [messageBtn setImage:[UIImage imageNamed:@"messaageInfo"] forState:UIControlStateNormal];
        [self addSubview:messageBtn];
        
       
        
        UILabel *line3 = [[UILabel alloc]initWithFrame:CGRectMake(5, h*3+40, frame.size.width-10, 0.3)];
        line3.backgroundColor = [UIColor grayColor];
        line3.alpha = 0.3;
        [self addSubview:line3];
        
         self.contentLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.titleLabel.frame.origin.x, line2.frame.origin.y+10, self.titleLabel.frame.size.width-30, h+20)];
         self.contentLabel.textColor = bgmgrayColor;
        self.contentLabel.numberOfLines = 0;
        self.contentLabel.font = [UIFont systemFontOfSize:14];
        [self addSubview:self.contentLabel];
        
        UIButton *titleBtn = [[UIButton alloc]initWithFrame:CGRectMake(15, line3.frame.origin.y, h, h)];
        [titleBtn setImage:[UIImage imageNamed:@"titleHome"] forState:UIControlStateNormal];
        [self addSubview:titleBtn];
        
        self.red1 = [[UILabel alloc]initWithFrame:CGRectMake(frame.size.width-130, titleBtn.frame.origin.y+(h-20)/2, 60, 20)];
        self.red1.backgroundColor = bgmColor(250, 241, 194);
         self.red1.textColor = bgmgrayColor;
        self.red1.font = [UIFont systemFontOfSize:10];
        self.red1.layer.cornerRadius = 10;
        self.red1.text = @"帮转红包";
        self.red1.layer.masksToBounds = YES;
        self.red1.textAlignment = NSTextAlignmentCenter;
        self.red1.layer.borderColor = bgmyelloeColor.CGColor;
        self.red1.layer.borderWidth = 1;
        [self addSubview:self.red1];
        
        
        self.red2 = [[UILabel alloc]initWithFrame:CGRectMake(frame.size.width-60, titleBtn.frame.origin.y+(h-20)/2, 35, 20)];
        self.red2.backgroundColor = bgmColor(250, 241, 194);
        self.red2.text = @"无偿";
        self.red2.textColor = bgmgrayColor;
        self.red2.font = [UIFont systemFontOfSize:10];
        self.red2.layer.cornerRadius = 10;
        self.red2.layer.masksToBounds = YES;
        self.red2.textAlignment = NSTextAlignmentCenter;
        self.red2.layer.borderColor = bgmyelloeColor.CGColor;
        self.red2.layer.borderWidth = 1;
        [self addSubview:self.red2];
        
        UILabel *line4 = [[UILabel alloc]initWithFrame:CGRectMake(5, h*4+40, frame.size.width-10, 0.3)];
        line4.backgroundColor = [UIColor grayColor];
        line4.alpha = 0.3;
        [self addSubview:line4];
        
        UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(15, line4.frame.origin.y, h, h)];
        [btn setImage:[UIImage imageNamed:@"guanxi"] forState:UIControlStateNormal];
        [self addSubview:btn];
        
        self.reletionShipLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.titleLabel.frame.origin.x, line4.frame.origin.y, self.titleLabel.frame.size.width, self.titleLabel.frame.size.height)];
        self.reletionShipLabel.textColor = bgmgrayColor;
        self.reletionShipLabel.numberOfLines = 0;
        self.reletionShipLabel.font = [UIFont systemFontOfSize:12];
        [self addSubview:self.reletionShipLabel];
        
        self.commentBtn = [[CommonBtn alloc]initWithFrame:CGRectMake(frame.size.width-120, line4.frame.origin.y+h/2-15, 50, 30)];
        [self.commentBtn setImage:[UIImage imageNamed:@"pinglun"] forState:UIControlStateNormal];
         [self.commentBtn addTarget:self action:@selector(clickLike:) forControlEvents:UIControlEventTouchUpInside];
        self.commentBtn.tag = 300;
        [self addSubview:self.commentBtn];
        
        self.forwardBtn = [[CommonBtn alloc]initWithFrame:CGRectMake(frame.size.width-70, line4.frame.origin.y+h/2-15, 50, 30)];
        [self.forwardBtn setImage:[UIImage imageNamed:@"zhuanfa"] forState:UIControlStateNormal];
         [self.forwardBtn addTarget:self action:@selector(clickLike:) forControlEvents:UIControlEventTouchUpInside];
        self.forwardBtn.tag = 400;

        [self addSubview:self.forwardBtn];
        
        x = (frame.size.width/2-h)/2;
        self.leftBtn = [[UIButton alloc]initWithFrame:CGRectMake(x, line4.frame.origin.y+h, h, h)];
        [self.leftBtn setImage:[UIImage imageNamed:@"zan_normal"] forState:UIControlStateNormal];
          [self.leftBtn setImage:[UIImage imageNamed:@"zan_sele"] forState:UIControlStateSelected];
        [self addSubview:self.leftBtn];
        self.leftBtn.tag = 100;
        [self.leftBtn addTarget:self action:@selector(clickLike:) forControlEvents:UIControlEventTouchUpInside];
        
        
        self.rightBtn = [[UIButton alloc]initWithFrame:CGRectMake(x+frame.size.width/2, line4.frame.origin.y+h, h, h)];
        [self.rightBtn setImage:[UIImage imageNamed:@"Thanks"] forState:UIControlStateNormal];
        
        [self.rightBtn setImage:[UIImage imageNamed:@"Thanks_sele"] forState:UIControlStateSelected];

        [self addSubview:self.rightBtn];
        self.rightBtn.tag = 200;
        [self.rightBtn addTarget:self action:@selector(clickLike:) forControlEvents:UIControlEventTouchUpInside];

 
        
        UITapGestureRecognizer *tap =[[ UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickBgView)];
        [self addGestureRecognizer:tap];
        

    }

    return self;
}




- (void)setMessageWithView:(NSDictionary *)dic changeData:(NSDictionary *)changeData{
    self.messgeDic = dic;
    
    self.nameLabel.text = [NSString stringWithFormat:@"%@",[dic objectForKey:@"userName"]];
    [self.iconBtn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"push_%@",[dic objectForKey:@"pTypeId"]]] forState:UIControlStateNormal];

    [self.addressBtn setTitle:[NSString stringWithFormat:@"%@",[dic objectForKey:@"pCity"]] forState:UIControlStateNormal];
    [self.timeBtn setTitle:[NSString stringWithFormat:@"%@",[dic objectForKey:@"pTime"]] forState:UIControlStateNormal];
    self.titleLabel.text = [NSString stringWithFormat:@"%@",[dic objectForKey:@"pTitle"]];
    self.contentLabel.text = [NSString stringWithFormat:@"%@",[dic objectForKey:@"pContent"]];
    self.reletionShipLabel.text = [NSString stringWithFormat:@"%@",[dic objectForKey:@"connections"]];
    [self.commentBtn setTitle:[NSString stringWithFormat:@"%@",[dic objectForKey:@"commentSize"]] forState:UIControlStateNormal];
    
    [self.forwardBtn setTitle:[NSString stringWithFormat:@"%@",[dic objectForKey:@"pFowordCount"]] forState:UIControlStateNormal];

    NSString *str = [NSString stringWithFormat:@"%@",[dic objectForKey:@"pRedPacakge"]];
    if ([str isEqualToString:@"0"]) {
        self.red1.hidden = YES;
    }else{
         self.red1.hidden = NO;
    }
    NSString *str1 = [NSString stringWithFormat:@"%@",[dic objectForKey:@"pitisPaid"]];
     if ([str1 isEqualToString:@"1"]) {
        self.red2.text = @"有偿";
    }else{
        self.red2.text = @"无偿";
    }

    
    str = [NSString stringWithFormat:@"%@",[changeData objectForKey:@"itisFollow"]];
    if ([str isEqualToString:@"YES"]) {
        self.leftBtn.selected = YES;
    }else{
        self.leftBtn.selected = NO;
    }
    
    str = [NSString stringWithFormat:@"%@",[changeData objectForKey:@"itisAppent"]];
    if ([str isEqualToString:@"YES"]) {
        self.rightBtn.selected = YES;
    }else{
        self.rightBtn.selected = NO;
    }
    
}
-(void)clickLike:(UIButton *)btn{
    int tag = (int)btn.tag;
    if(tag ==100){  //收藏
        btn.selected = !btn.selected;
        [self.homeDelegate clickHomeBtn:btn dic:self.messgeDic];
    }else if (tag == 200){ //受理
        if (self.rightBtn.selected) {
            [SVProgressHUD showInfoWithStatus:@"您已经受理啦！"];
        }else{
            [self.homeDelegate clickAccept];
        }
    }else if (tag == 300){ //评论
        [self.homeDelegate clickCommon];
    }else if (tag == 400){  //转发
        [self.homeDelegate clickforward];

    }
}

-(void)clickBgView{
    [self.homeDelegate gotoMessageInfo:self.messgeDic];
}
@end
